package akshay.drawing;

import java.util.Vector;

import akshay.drawing.EditActivity;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Region;
import android.graphics.Path.FillType;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class Display extends View{	
	public static float x=100,y=100;
	private static boolean isCircle=false;
	private static boolean isRect=false;
	public static boolean isLongclick=false,ismove=false;
	public static int countCircle=0;
	public static boolean iscirclenew;
	static Vector<Circle> al=new Vector<Circle>();
	private Context myContext;
	static Circle c;
	Rectangle r=new  Rectangle();
	Paint paint,paint1;
	static int id1=1;
	Region region;
	
	Path path;
	private OnLongClickListener mLongClickListener = 
		new OnLongClickListener() {

		public boolean onLongClick(View view) {
			int countcircle1=0;
			while(al.size()>countcircle1){
				c=(Circle)al.get(countcircle1).getCircle();
				if((x>c.x-50&&y>c.y-50) && (x<c.x+50&&y<c.y+50)){
					c.setMoving(true);
					isLongclick=true;
					countcircle1++;
					Log.d("onlongclick", ""+countcircle1);
					break;
				}
				else{
					countcircle1++;
				}
			}
			Toast.makeText(getContext(), "onlongclick", Toast.LENGTH_SHORT).show();
			Notification n=new Notification();
			n.ledARGB = Color.RED;
			n.vibrate=new long[] { 1000, 2500, 1000, 5000};
			//invalidate();
			return true;
		}
		
	};
	public Display(Context context) {
        super(context);
         myContext =context;
        this.setOnLongClickListener(mLongClickListener);
        this.setOnTouchListener(mTouchListener);
    }
	@Override  
    protected void onDraw(Canvas canvas) {
	  super.onDraw(canvas);
	  canvas.drawColor(Color.BLACK);
	  path = new Path();
	  paint=new Paint();
	  //paint.setColor(Color.GREEN);
	  if(iscirclenew){
		  Circle c1=new Circle();
		  paint=new Paint();
		  paint.setAntiAlias(true);
		  paint.setColor(Color.GREEN);
		  paint.setStrokeWidth(3);
		  paint.setStyle(Paint.Style.FILL_AND_STROKE);
		  c1.setCircle(100, 100, 50, paint);
		  c1.setId(id1++);
		  path.setFillType(FillType.EVEN_ODD);
		  path.addCircle(c1.x,c1.y, 50, Path.Direction.CCW);
		  canvas.drawPath(path , c1.p);
		  al.add(c1);
		  isCircle=true;
		  countCircle++;
		  iscirclenew=false;
	  }
	  
	  
	  if(isCircle){
		  if(isLongclick){
		  
		  for(int circlecount1=0;circlecount1<countCircle;circlecount1++)
		  {
			  
			  Circle c1=(Circle)al.get(circlecount1).getCircle();
		  if((c1.x==c.x)&&c1.getMoving()){
			  c1.setCircle(x,y, c1.r, c1.p);
			  Log.d("count", "no"+circlecount1);
			  path.addCircle(c1.x,c1.y, c1.r, Path.Direction.CCW); 
			  canvas.drawPath(path , c1.p);
			  canvas.clipPath(path, Region.Op.DIFFERENCE);
			  canvas.save();
		  }
		  c1.setCircle(c1.x,c1.y, c1.r, c1.p);
		  path.addCircle(c1.x,c1.y, c1.r, Path.Direction.CCW); 
		  canvas.drawPath(path , c1.p);
		  canvas.clipPath(path, Region.Op.DIFFERENCE);
		  canvas.save();
		  }
		  }
	  
	  else{
		  if(countCircle>0)
		  {
			  for(int i=0;i<countCircle;i++){
				  Circle c1=al.get(i).getCircle();
				  
				  if(c1.p!=null){
					  path.addCircle(c1.x,c1.y, c1.r, Path.Direction.CCW); 
					  canvas.drawPath(path , c1.p);
					  canvas.clipPath(path, Region.Op.DIFFERENCE);
				  }
			  }
		  }
	  }
	  }
	  
	  //drawing rectangle
	  if(isRect){
		  paint.setColor(Color.GREEN);
			  //r.setRect(x, y, 50, paint);
			  path.addRect(10, 10, 100, 100, Path.Direction.CCW); 
			  canvas.drawPath(path , paint);
			 
			  canvas.clipPath(path, Region.Op.DIFFERENCE);
			  canvas.save();
		  
		  
		  }

    }
  int occur=0;
  private OnTouchListener mTouchListener = 
		new OnTouchListener() {
		public boolean onTouch(View view, MotionEvent event) {
			x=event.getX();
	    	 y=event.getY();
	    	 boolean consumed=false;
			switch(event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				isLongclick=false;
		       	//Toast.makeText(getContext(), "down......", Toast.LENGTH_SHORT).show();
		        consumed=false;
		        break;
			case MotionEvent.ACTION_MOVE:
		       	//Toast.makeText(getContext(), "move......", Toast.LENGTH_SHORT).show();
				Log.d("move", "Moving");
					invalidate();
				consumed = true;
				break;
			case MotionEvent.ACTION_UP:
				occur=1;
				uncheck();
				if(isLongclick!=true){
					if(check()){
						Intent newintent = new Intent();
				        newintent.setClass(myContext,EditActivity.class);
				        Log.d("activity", "before act");
				        newintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				        
				        //Log.d("activity", "after act");
				        
				        Bundle extras = new Bundle();
				        extras.putString("Name",c.getShape());
				        extras.putString("ID",Integer.toString(c.getId()));
				        newintent.putExtras(extras);
				        myContext.startActivity(newintent);
					}
				}
				
				isLongclick=false;
		       	//Toast.makeText(getContext(), "up......", Toast.LENGTH_SHORT).show();
		       	consumed = true;
		       	break;
			}
			return false;
		}			
	};
	void setCircle(boolean isCircle){
		this.isCircle=isCircle;
	}
	void setRect(boolean isRect){
		this.isRect=isRect;
	}
	
	//to check user long click inside circle or not. 
	boolean check(){
		int countcircle1=0;
		while(al.size()>countcircle1){
			Circle	c1=al.get(countcircle1).getCircle();
			if((x>c1.x-50&&y>c1.y-50) && (x<c1.x+50&&y<c1.y+50) || isLongclick){
				countcircle1++;
				return true;
				}
			else{
				countcircle1++;
			}
			}
		return false;
	}
	
	
	void uncheck(){
		int countcircle1=0;
		while(al.size()>countcircle1){
			c=al.get(countcircle1).getCircle();
			c.setMoving(false);
				countcircle1++;
		}
	}
	
	//get click event for current circle
	public Circle getCurrentCircle(){
		if(c.equals(null)){
			return new Circle();
		}
		else{
			return c;
		}
	}
}